import { Observable } from 'rxjs'
import { Request } from 'express'
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'
import { Reflector } from '@nestjs/core'
import { UnauthorizedException } from '@nestjs/common'

@Injectable()
export class AdminGuard implements CanActivate {

  constructor(
    private reflector: Reflector
  ) { }

  canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
    let http = context.switchToHttp()
    let request = http.getRequest<Request>()
    // 还需要判断用户是否存在，这里偷懒就没写了
    if (!request.jwt || new Date().getTime() >= request.jwt.exp * 1000) {
      throw new UnauthorizedException()
    }
    return true
  }

}